import {
  mapState,
  mapMutations,
  mapActions
} from "vuex";

export default {
  computed: {
    ...mapState("common", {
      userInfo: (state) => state.userInfo,
      goeasyObj: (state) => state.goeasyObj,
      conversationList: (state) => state.conversationList,

    }),
  },
  methods: {
    ...mapMutations("common", [
      "updata_ConversationList",
    ]),
    ...mapMutations("quiz", ["set_entering"]),
    ...mapActions("common", [
      "createZIM",
      "ZIMLogin",
      "getUserInfo2",
    ]),

    getZIM() {
      let that = this;
      // 注册监听“运行时错误信息”的回调
      this.zim.on("error", function (zim, errorInfo) {
        // console.log("error", errorInfo.code, errorInfo.message);
      });

      // 更新会话列表
      this.zim.on("conversationChanged", function (zim, {
        infoList
      }) {
        // console.log("更新会话列表conversationChanged", infoList);

        let conversationList = Object.assign([], that.conversationList)

        for (let item of infoList) {
          let is = false
          if (item.lastMessage) {
            let extendedData = JSON.parse(item.lastMessage.extendedData)
            if (extendedData.quiz_id) {
              that.zim.deleteConversation(item.conversationID, 0, {
                isAlsoDeleteServerConversation: true
              })
              continue;
            }
          }

          for (let i = 0; i < conversationList.length; i++) {
            if (conversationList[i].conversationID == item.conversation.conversationID) {
              conversationList.splice(i, 1, item.conversation)
              is = true
            }
          }
          if (!is) conversationList.push(item.conversation)

        }

        conversationList = conversationList.sort((obj1, obj2) => {
          return obj2.orderKey - obj1.orderKey;
        })

        that.updata_ConversationList(conversationList);
      });

      // 注册监听“网络连接状态变更”的回调
      this.zim.on(
        "connectionStateChanged",
        function (zim, {
          state,
          event,
          extendedData
        }) {
          // console.log(
          //     "connectionStateChanged",
          //     state,
          //     event,
          //     extendedData
          // );
          // 当长时间网络连接断开导致SDK内部登出时，需要重新登录
          if (state == 0 && event == 3) {
            // zim.login(userInfo, token);
            console.log("网络连接状态变更");
            that.ZIMLogin(that.zim).then((zim_l) => {
              that.zim = zim_l;
            });
          }
        }
      );

      // 注册监听“令牌即将过期”的回调
      this.zim.on("tokenWillExpire", function (zim, {
        second
      }) {
        // console.log("令牌即将过期", second);
        // 可以在这里调用 renewToken 接口来更新 token
        // 新 token 生成可以参考上文
        let token = localStorage.getItem("zegoToken");
        localStorage.removeItem("zegoToken");
        zim.renewToken(token)
          .then(function ({
            token
          }) {
            // 更新成功
            localStorage.setItem("zegoToken", token);
            that.ZIMLogin(that.zim).then((zim_l) => {
              that.zim = zim_l;
            });
          })
          .catch(function (err) {
            // 更新失败
            that.ZIMLogin(that.zim).then((zim_l) => {
              that.zim = zim_l;
            });
          });
      });

    },



  }
};
